


//没有完善

import UIKit

class CheckViewController: BaseViewController {

    var head : TestPhoneModelView?
    
    var screenFunc : ScreenFuncView?
    
    var mod : PhoneInfoModel?
    
    var baseFunc : ScreenFuncView?
    
    var goodsID : Int = -1
    
    var decreaseItems : Array<DecreaseItemModel> = []
    //进入该界面时请求这个手机的价格区间并显示
    override func viewDidLoad() {
        super.viewDidLoad()
        self.naviHide = false
        self.title = "检测外观"
        self.tabBarController?.tabBar.isHidden = true
        self.defaultBackButton()
        configSubView()
        loadRangeData()
    }
    
    //请求当前手机的价格区间
    func loadRangeData() -> Void {
        guard mod != nil else {
            return
        }
        let param = ["trademark":mod!.trademark,"phone_model":mod!.phone_model,"internal_storage":mod!.internal_storage]
        self.beginAnim()
        Manager.shared().estimatePhone(param: param, success: {(model) in
            self.endAnim()
            if let dataDic = model.data as? Dictionary<String,Any>{
                if let hightPrice = dataDic["highprice"] as? Int {
                    if let lowPrice = dataDic["lowprice"] as? Int {
                        let highPric = Float(hightPrice / 100)
                        let lowPric = Float(lowPrice / 100)
                        let range = String(lowPric) + "-" + String(highPric)
                        self.head!.priceRange = range
                    }
                }
            }
        }, fail: {(code) in
            self.endAnim()
            self.commonAlertMsg(content: YXNetWorkFail)
        })
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    private func configSubView()->Void{
        head = TestPhoneModelView.init(frame: CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: YXDeviceWidth, height: 200 * YXHeightScale)))
        if mod != nil {
            head!.title = mod!.phone_model
        }
        
        screenFunc = ScreenFuncView.init(frame: CGRect.init(origin: CGPoint.init(x: 10 * YXWidthScale, y: 0), size: CGSize.init(width: YXDeviceWidth - 20 * YXWidthScale, height: 278 * YXHeightScale)))
        
        let scroll = UIScrollView.init(frame: CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: YXDeviceWidth, height: YXDeviceHeight - 140 * YXHeightScale - 64)))
        self.view.addSubview(scroll)
        scroll.backgroundColor = UIColor.colorWithHexString(hex: "ececec")
        scroll.addSubview(head!)//scroll上有一个head
        
        let tray = UIView.init(frame: CGRect.init(origin: CGPoint.init(x: 0, y: 206 * YXHeightScale), size: CGSize.zero))
        tray.backgroundColor = UIColor.colorWithHexString(hex: "f3f3f5")
        scroll.addSubview(tray)
        screenFunc!.addHead(title: "屏幕功能")
        screenFunc!.addBlock(title: "1.显示", titleArr: ["屏幕显示正常","有坏点/亮点/色差","内屏进水","严重老化/漏液/错乱"], key: "screen_show", multiChoose: true,extra: true)
        screenFunc!.addBlock(title: "2.触摸", titleArr: ["屏幕触摸正常","屏幕触摸不正常"], key: "touch_error", multiChoose: false,extra: false)
        if mod?.screen_show != -1 {
            //8种可能?
            if mod?.screen_show == 0 {
                screenFunc?.lightAButton(blockIdx: 0, ButtonIdx: 0)
            } else if mod?.screen_show == 1 {
                screenFunc?.lightAButton(blockIdx: 0, ButtonIdx: 1)
            } else if mod?.screen_show == 2 {
                screenFunc?.lightAButton(blockIdx: 0, ButtonIdx: 3)
            } else if mod?.screen_show == 3 {
                screenFunc?.lightAButton(blockIdx: 0, ButtonIdx: 2)
            }else if mod?.screen_show == 12 {
                screenFunc?.lightAButton(blockIdx: 0, ButtonIdx: 1)
                screenFunc?.lightAButton(blockIdx: 0, ButtonIdx: 3)
            }else if mod?.screen_show == 13 {
                screenFunc?.lightAButton(blockIdx: 0, ButtonIdx: 1)
                screenFunc?.lightAButton(blockIdx: 0, ButtonIdx: 2)
            }else if mod?.screen_show == 23 {
                screenFunc?.lightAButton(blockIdx: 0, ButtonIdx: 3)
                screenFunc?.lightAButton(blockIdx: 0, ButtonIdx: 2)
            }else if mod?.screen_show == 123 {
                screenFunc?.lightAButton(blockIdx: 0, ButtonIdx: 1)
                screenFunc?.lightAButton(blockIdx: 0, ButtonIdx: 2)
                screenFunc?.lightAButton(blockIdx: 0, ButtonIdx: 3)
            }
        }
        if mod?.screen_touch != -1 {
            if mod?.screen_touch == 0 {
                screenFunc?.lightAButton(blockIdx: 1, ButtonIdx: 0)
            } else if mod?.screen_touch == 1 {
                screenFunc?.lightAButton(blockIdx: 1, ButtonIdx: 1)
            }
        }
        screenFunc!.configFrame(origin: CGPoint.init(x: 10 * YXWidthScale, y: 0), width: YXDeviceWidth - 20 * YXWidthScale)
        tray.addSubview(screenFunc!)
        tray.frame = CGRect.init(origin: CGPoint.init(x: 0, y: 206 * YXHeightScale), size: CGSize.init(width: YXDeviceWidth, height: screenFunc!.bottom()))
        
        baseFunc = ScreenFuncView.init(frame: CGRect.init(origin: CGPoint.init(x: 0, y: screenFunc!.bottom()), size: CGSize.init(width: YXDeviceWidth - 20 * YXWidthScale, height: 0)))
        baseFunc!.addHead(title: "基本情况")
        baseFunc!.delegated = true
        baseFunc!.addBlock(title: "3.具体型号", titleArr: ["国行","港版","水货无锁版"], key: "channel", multiChoose: false);//与接口字段匹配,该字段将作为获取商品id接口的参数
        baseFunc!.numberSeted = 3
        baseFunc!.addBlock(title: "4.保修情况", titleArr: ["超保","未超保"], key: "warranty_period", multiChoose: false)//超保与否
        baseFunc!.addBlock(title: "5.网络类型", titleArr: ["全网通","其他"], key: "network_type", multiChoose: false)
        baseFunc!.completionBlock = {
            //请求网络地址
            self.getFinalPrice()
        }
        baseFunc!.configFrame(origin: CGPoint.init(x: 10 * YXWidthScale, y: baseFunc!.bottom() + 10 * YXHeightScale), width: YXDeviceWidth - 20 * YXWidthScale)
        tray.frame = CGRect.init(origin: CGPoint.init(x: 0, y: 206 * YXHeightScale), size: CGSize.init(width: YXDeviceWidth, height: baseFunc!.bottom()))
        tray.addSubview(baseFunc!)
        scroll.contentSize = CGSize.init(width: 0, height: tray.bottom())
        self.automaticallyAdjustsScrollViewInsets = false
        let base = UIView.init(frame: CGRect.init(x: 0, y: self.view.height() - 140 * YXHeightScale - 64, width: YXDeviceWidth, height: 140 * YXHeightScale))
        base.backgroundColor = UIColor.white
        self.view.addSubview(base)
        let estimate = UIButton.init(type: .system)
        estimate.setTitle("下一步", for: .normal)
        estimate.backgroundColor = UIColor.black
        estimate.titleLabel?.font = UIFont.systemFont(ofSize: 36 * YXWidthScale)
        estimate.setTitleColor(UIColor.white, for: .normal)
        estimate.layer.cornerRadius = YXButtonCorner
        estimate.frame = CGRect.init(x: (YXDeviceWidth - 480 * YXWidthScale) / 2, y: (base.height() - 108 * YXHeightScale) / 2, width: 480 * YXWidthScale, height: 108 * YXHeightScale)
        estimate.addTarget(self, action: #selector(estimated), for: .touchUpInside)
        base.addSubview(estimate)
    }
    
    func getFinalPrice() -> Void {
        
        var param : Dictionary<String,String> = [:]
        if let mark = mod?.trademark {
            param["trademark"] = mark
        }
        if let model = mod?.phone_model {
            param["phone_model"] = model
        }
        if let storage = mod?.internal_storage {
            param["internal_storage"] = storage
        }
        let array2 = baseFunc!.provideAllParam()
        
        for item in array2 {
            if item.keys.contains("channel") {
                let channel = item["channel"] as! String
                if channel == "0" {
                    param["channel"] = "国行"
                } else if channel == "1" {
                    param["channel"] = "港版"
                } else if channel == "2" {
                    param["channel"] = "其他"
                }
            }
            
            if item.keys.contains("warranty_period") {
                //1,0
                let warrant = item["warranty_period"] as! String
                if warrant == "0" {
                    param["warranty_period"] = "1"
                } else if warrant == "1" {
                    param["warranty_period"] = "0"
                }
            }
            
            if item.keys.contains("network_type") {
                let type = item["network_type"] as! String
                if type == "0" {
                    param["network_type"] = "全网通"
                }else if (type == "1"){
                    param["network_type"] = "其他"
                }
            }
        }
        beginAnim()
        Manager.shared().getGoodsInfo(param: param, success: {(model) in
            self.endAnim()
            let mod = model as? GoodsModel
            if mod?.result == true {
                self.goodsID = mod!.goodsID
                self.decreaseItems = mod!.decreaseItems
            } else {
                self.commonAlertMsg(content: mod!.msg)
            }
        }, fail: {(code) in
            self.endAnim()
            self.commonAlertMsg(content: YXNetWorkFail)
        })
    }
    
    @objc func estimated() -> Void {
        if goodsID != -1 {
            let arr = screenFunc!.provideAllParam()
            let estimatedVC = LookingTestingViewController.init()
            estimatedVC.mod = mod
            estimatedVC.price_range = head!.priceRange
            estimatedVC.goods_id = goodsID
            estimatedVC.decreaseItems = self.decreaseItems
            for item in arr {
                if item.keys.contains("screen_show") {
                    if var screenDisplay = item["screen_show"] as? Array<String> {
                        if screenDisplay.count == 0 {
                            commonAlertMsg(content: "请确认屏幕显示是否正常!")
                            return
                        }
                        //0 UI上面标示正常 1不正常
                        if screenDisplay.first! == "0" {
                            estimatedVC.screen_display = "0"
                        } else {
                            let idx = screenDisplay.index(of: "3")
                            let idx1 = screenDisplay.index(of: "2")
                            if idx != nil {
                                screenDisplay[idx!] = "2"
                            }
                            if idx1 != nil {
                                screenDisplay[idx1!] = "3"
                            }
                            screenDisplay = screenDisplay.sorted()
                            estimatedVC.screen_display = screenDisplay.joined(separator: "")
                        }
                        mod?.screen_show = StringToInt.toInt(source: estimatedVC.screen_display)
                    }
                    }
                if item.keys.contains("touch_error") {
                    let touch = item["touch_error"] as! String
                    if touch == "" {
                        commonAlertMsg(content: "请确认屏幕触摸是否正常!")
                        return
                    }
                    if touch == "0" {
                        estimatedVC.touch_error = "0"
                    } else if touch == "1" {
                        estimatedVC.touch_error = "1"
                    }
                }
            }
            let array2 = baseFunc!.provideAllParam()
            for item in array2 {
                if item.keys.contains("channel") {
                    let channel = item["channel"] as! String
                    if channel == "" {
                        commonAlertMsg(content: "请确认发型版本!")
                        return
                    }
                }
                if item.keys.contains("warranty_period") {
                    let bao = item["warranty_period"] as! String
                    if bao == "" {
                        commonAlertMsg(content: "请选择是否超保!")
                        return
                    }
                }
                if item.keys.contains("network_type") {
                    let type = item["network_type"] as! String
                    if type == "" {
                        commonAlertMsg(content: "请选择网络类型!")
                        return
                    }
                }
            }
            self.navigationController?.pushViewController(estimatedVC, animated: true)
        } else {
            commonAlertMsg(content: "尚未获取商品数据,请选取手机参数后重试");
        }
    }

}
